package ru.poomsae.isap.Offline.SystemCutoff

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Vibrator
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.example.isap_wtf.Model.zaprosJSON
import ru.poomsae.isap.Offline.MenuActivity
import ru.poomsae.isap.Online.SystemCut.PresentationActivity
import ru.poomsae.isap.R

class SystemOfCutOFF : AppCompatActivity() {

    var min03: Int = 0
    var min01: Int = 0
    var accyracy: Int = 40
    var accyracyDouble: Double = 4.0
    lateinit var vibrator: Vibrator
    lateinit var myTextView: TextView
    lateinit var myLinearLayout: LinearLayout
    lateinit var myRelativeLayout: RelativeLayout
    lateinit var mySudTextView: TextView
    lateinit var myPoomsaeTextView: TextView
    lateinit var myzaprosJSON: zaprosJSON

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.sys_of_cut)

        myTextView = findViewById(R.id.idvistText) as TextView
        myLinearLayout = findViewById(R.id.progressBar) as LinearLayout
        myRelativeLayout = findViewById(R.id.system_of_cut) as RelativeLayout
        mySudTextView = findViewById(R.id.sudNumTV) as TextView
        myPoomsaeTextView = findViewById(R.id.phumTV)

        myzaprosJSON = zaprosJSON(myTextView, myLinearLayout, myRelativeLayout, mySudTextView, myPoomsaeTextView)
        val decorView = window.decorView
        val uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN
        mySudTextView.visibility = View.GONE
        myPoomsaeTextView.visibility = View.GONE

        val btn03 = findViewById(R.id.btn03) as Button
        val btn01 = findViewById(R.id.btn01) as Button
        val btnRes = findViewById(R.id.btnRes) as Button
        val btnCan03 = findViewById(R.id.btnCan03) as Button
        val btnCan01 = findViewById(R.id.btnCan01) as Button
        val btnPresentation = findViewById(R.id.btnPresentation) as Button
        val tView03 = findViewById(R.id.tView03) as TextView
        val tView01 = findViewById(R.id.tView01) as TextView

        vibrator = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
        btn03.setOnClickListener { if (accyracy <= 40 && accyracy > 0) { vibrator.vibrate(200); min03 = min03 + 1; scoring() } }
        btn01.setOnClickListener {  if (accyracy <= 40 && accyracy > 0) { vibrator.vibrate(200); min01 = min01 + 1; scoring() } }
        btnRes.setOnClickListener { min03 = 0; min01 = 0; accyracy = 40; scoring(); tView03.setText(min03.toString() + " x "); tView01.setText(" x " + min01.toString()) }
        btnCan03.setOnClickListener { if (accyracy < 40 && min03 > 0) {min03 = min03 - 1; scoring(); tView03.setText(min03.toString() + " x ")} }
        btnCan01.setOnClickListener { if (accyracy < 40 && min01 > 0) {min01 = min01 - 1; scoring(); tView01.setText(" x " + min01.toString())} }
        btnPresentation.setOnClickListener {
            val intent = Intent (this, PresentationActivity::class.java)
            intent.putExtra("accuracyDouble", accyracyDouble)
            startActivity(intent)
        }
    }

    fun scoring() {
        val tvAccyrasy = findViewById(R.id.tvAccyrasy) as TextView
        val tView01 = findViewById(R.id.tView01) as TextView
        val tView03 = findViewById(R.id.tView03) as TextView
        accyracy = 40 - min01 - min03*3
        accyracyDouble = accyracy.toDouble()/10
        tvAccyrasy.setText(accyracyDouble.toString())
        tView01.setText(" x " + min01.toString())
        tView03.setText(min03.toString() + " x ")

        if (accyracy < 0) {
            accyracyDouble = 0.0
            tvAccyrasy.setText(accyracyDouble.toString())
        }
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_off, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item?.itemId) {
            R.id.menu_offline -> {
                val intent = Intent (this, MenuActivity::class.java)
                startActivity(intent)
            }

        }

        return true
    }

}